using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
namespace Super
{
	/// <summary>
	/// MenuItem    
	/// itemName	显示路径
	/// bool		是否是条件检测函数
	/// priority	优先级越大，在菜单列表中越靠下；
	/// 同一列表相邻两条目优先级差距大于10会自动产生一条横线 
	/// </summary>
	public class GOMenuExtension : EditorWindow
	{
		[MenuItem("GameObject/Super/GetPath", true)]
		[MenuItem("GameObject/Super/Select_GO_Button", true)]
		public static bool CheckIfSelectGameObject()
		{
			GameObject selectedObject = Selection.activeGameObject;
			return selectedObject != null;
		}

		[MenuItem("GameObject/Super_Create/Widget", true)]
		[MenuItem("GameObject/Super_Create/Panel", true)]
		[MenuItem("GameObject/Super_Createt/PanelAndWidget", true)]
		[MenuItem("GameObject/Super_Create/AsyncImage", true)]
		[MenuItem("GameObject/Super_Create/HangScriptAndSavePrefab", true)]
		[MenuItem("GameObject/Super_Create/DeletePrefabAndScript", true)]
		public static bool CheckIfSelectGameObject2()
		{
			GameObject selectedObject = Selection.activeGameObject;
			return selectedObject != null;
		}

		/// <summary>
		/// 获取相对于panel或widget或UIItemList的路径
		/// </summary>
		/// <param name="command"></param>
		[MenuItem("GameObject/Super/GetPath", false, 0)]
		public static void GetPath(MenuCommand command)
		{
			GameObject item = command.context as GameObject;
			StringBuilder SB = new StringBuilder();
			SB.Append(item.name);
			Transform parent = item.transform.parent;
			bool isRoot = parent && (parent.name.Contains("Widget") || parent.name.Contains("Panel"));
			bool isItemList = parent && parent.GetComponent<UIItemList>() != null;
			string lastName = "";

			while (parent != null && !isRoot && !isItemList)
			{
				SB.Insert(0, "/");
				SB.Insert(0, parent.name);
				lastName = parent.name;
				parent = parent.parent;
				isRoot = parent && (parent.name.Contains("Widget") || parent.name.Contains("Panel"));
				isItemList = parent && parent.GetComponent<UIItemList>() != null;
			}

			if (isItemList && !string.IsNullOrEmpty(lastName))
			{
				SB.Remove(0, lastName.Length + 1);
			}
			EditorGUIUtility.systemCopyBuffer = SB.ToString();
		}

		/// <summary>
		/// 生成并挂载脚本，并且保存预制体
		/// </summary>
		[MenuItem("GameObject/Super_Create/HangScriptAndSavePrefab", false, 10)]
		public static void HangScriptAndSavePrefab()
		{
			GameObject selectedObject = Selection.activeGameObject;
			string GO_Name = selectedObject.name;
			bool isPanel = GO_Name.Contains("Panel");
			string classText = isPanel ? TextUtil.GetTemplatePanelClassText(GO_Name) : TextUtil.GetTemplateWidgetClassText(GO_Name);
			string path = isPanel ? FWConfig.panelScriptPath : FWConfig.widgetScriptPath;
			path = string.Format(path, GO_Name);
			AssetUtil.CreateFile(path, classText);

			List<object> arguments = new List<object>() { path, GO_Name };
			ReloadScriptManager.Ins.AddMethod("HangScriptSavePrefab", arguments, true);

			AssetDatabase.Refresh();
		}

		/// <summary>
		/// 删除预制体和脚本，针对panel和widget
		/// </summary>
		[MenuItem("GameObject/Super_Create/DeletePrefabAndScript", false, 30)]
		public static void DeletePrefabAndScript()
		{
			GameObject selectedObject = Selection.activeGameObject;
			string GO_Name = selectedObject.name;
			bool isPanel = GO_Name.Contains("Panel");
			string prefabPathKey = isPanel ? FWConfig.panelPrefabPath : FWConfig.widgetPrefabPath;
			string scriptPathKey = isPanel ? FWConfig.panelScriptPath : FWConfig.widgetScriptPath;
			string prefabPath = string.Format(prefabPathKey, GO_Name);
			string scriptPath = string.Format(scriptPathKey, GO_Name);

			GameObject.DestroyImmediate(selectedObject);
			AssetDatabase.DeleteAsset(prefabPath);
			AssetDatabase.DeleteAsset(scriptPath);
			AssetDatabase.Refresh();
		}

		/// <summary>
		/// 为无选中对象时，测试按钮
		/// </summary>
		/// <param name="command"></param>
		[MenuItem("GameObject/Super/FuncButton", false, 90000)]
		public static void FuncButton(MenuCommand command)
		{

		}

		/// <summary>
		/// 选中对象时，测试按钮
		/// </summary>
		/// <param name="command"></param>
		[MenuItem("GameObject/Super/Select_GO_Button", false, 90001)]
		public static void FuncSelect_GO_Button(MenuCommand command)
		{
			GameObject selectedObject = Selection.activeGameObject;
			string selectName = selectedObject.name;
			if (selectName.Contains("Panel"))
			{
				//GameUtil.ClosePanel(selectName);
			}
		}

		/// <summary>
		/// 创建widget模板
		/// </summary>
		[MenuItem("GameObject/Super_Create/Widget", false, -1)]
		public static void CreateWidget()
		{
			GameObject selectedObject = Selection.activeGameObject;
			GameObject prefabs = AssetUtil.ALoadAsset<GameObject>(FWConfig.templateWidget);
			GameObject go = GameObject.Instantiate(prefabs, selectedObject.transform, false);
			go.name = "templateWidget";
			SceneHierarchyUtility.SetExpanded(selectedObject, true);
			SceneHierarchyUtility.Expend(go.transform, true);

		}

		/// <summary>
		/// 创建panel模板
		/// </summary>
		[MenuItem("GameObject/Super_Create/Panel", false, 1)]
		public static void CreatePanel()
		{
			GameObject selectedObject = Selection.activeGameObject;
			GameObject prefabs = AssetUtil.ALoadAsset<GameObject>(FWConfig.templatePanel);
			GameObject go = GameObject.Instantiate(prefabs, selectedObject.transform, false);
			go.name = "templatePanel";
			SceneHierarchyUtility.SetExpanded(selectedObject, true);
			SceneHierarchyUtility.Expend(go.transform, true);
		}

		/// <summary>
		/// 创建panel和widget模板
		/// </summary>
		[MenuItem("GameObject/Super_Create/PanelAndWidget", false, 2)]
		public static void CreatePanelAndWidget()
		{
			GameObject selectedObject = Selection.activeGameObject;
			GameObject panelPrefabs = AssetUtil.ALoadAsset<GameObject>(FWConfig.templatePanel);
			GameObject panelGO = GameObject.Instantiate(panelPrefabs, selectedObject.transform, false);
			panelGO.name = "templatePanel";

			GameObject widgetPrefabs = AssetUtil.ALoadAsset<GameObject>(FWConfig.templateWidget);
			GameObject widgetGO = GameObject.Instantiate(widgetPrefabs, panelGO.transform.Find("Root").transform, false);
			widgetGO.name = "templateWidget";
			SceneHierarchyUtility.SetExpanded(selectedObject, true);
			SceneHierarchyUtility.Expend(panelGO.transform, true);
		}

		/// <summary>
		/// 创建带有AsyncImage的游戏对象
		/// </summary>
		[MenuItem("GameObject/Super_Create/AsyncImage", false, 50)]
		public static void CreateAsyncImage()
		{
			GameObject selectedObject = Selection.activeGameObject;
			GameObject go = new GameObject("AsyncImage");
			go.AddComponent<Image>();
			go.AddComponent<AsyncImage>();
			go.layer = LayerMask.NameToLayer("UI");
			go.transform.SetParent(selectedObject.transform, false);
			SceneHierarchyUtility.SetExpanded(selectedObject, true);
			SceneHierarchyUtility.Expend(go.transform, true);
		}
	}
}
